package com.jwthhealth_pub.old.report.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.report.model.resbody.SportSuggListResp;
import com.jwthhealth_pub.old.report.view.adapter.SportSuggDescAdapter;
import com.jwthhealth_pub.old.report.view.adapter.SportsuggDayPlanAdapter;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;

/**
 * 运动建议
 * Created by liuzhen on 2017/10/23.
 */

public class SportSuggestActivity extends BaseActivity {

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.rv_desc)
    RecyclerView rvDesc;
    @BindView(R.id.rv_plan)
    RecyclerView rvPlan;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sport_suggest);
        ButterKnife.bind(this);

        String id = getIntent().getStringExtra("id");
        if (id != null)
            getData(id);
    }

    @Override
    public void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    public void getData(String id) {
        LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        if (userinfoModel == null) return;

        String uid = userinfoModel.getId();
        String androidtoken = userinfoModel.getAndroidtoken();
        if (uid == null || androidtoken == null)
            return;

        final Call<SportSuggListResp> _call = ApiHelper.getSportSuggList(id, uid, androidtoken);
        Observable<SportSuggListResp.DataBeanX> observable = Observable.create(new ObservableOnSubscribe<SportSuggListResp.DataBeanX>() {
            @Override
            public void subscribe(ObservableEmitter<SportSuggListResp.DataBeanX> e) throws Exception {

                showLoadProgresbar();

                SportSuggListResp body = _call.execute().body();
                if (body == null) {
                    e.onError(new Exception(getString(R.string.common_net_error)));
                    return;
                }

                String code = body.getCode();
                if (code.equals("0")) {
                    SportSuggListResp.DataBeanX data = body.getData();
                    if (data != null) {
                        e.onNext(data);
                    } else {
                        e.onError(new Exception(getString(R.string.common_net_error)));
                    }
                } else {
                    e.onError(new Exception(body.getMsg()));
                }

            }
        });

        Observer<SportSuggListResp.DataBeanX> observer = new Observer<SportSuggListResp.DataBeanX>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SportSuggListResp.DataBeanX o) {
                missLoadProgressbar();
                //已经过期
//                initUI(o);
                if (o.getDesc() != null && o.getDesc().size() > 0)
                    initDesc(o.getDesc());
//
                if (o.getDay_list() != null && o.getDay_list().size() > 0)
                    initDayPlan(o.getDay_list());
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };

        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
    }

    /**
     * 加载训练计划
     *
     * @param day_list data
     */
    private void initDayPlan(List<SportSuggListResp.DataBeanX.DayListBean> day_list) {
        rvPlan.setLayoutManager(new LinearLayoutManager(this));
        rvPlan.setAdapter(new SportsuggDayPlanAdapter(this, day_list));
    }

    /**
     * 加载表述列表
     *
     * @param desc data
     */
    private void initDesc(List<SportSuggListResp.DataBeanX.DescBean> desc) {
        rvDesc.setLayoutManager(new LinearLayoutManager(this));
        rvDesc.setAdapter(new SportSuggDescAdapter(this, desc));
    }
}
